<?php
	class User extends MY_controller{

		function __construct(){
			parent::__construct();
			$this->load->model('users');
			$this->load->library('redis',array('model'=>'anchor_redis','channel'=>'live'),'anchor_redis');
			//$cdn_list=get_list_field_by_where('user_anchor_cdn','*',"status=1");
			
			// $this->smarty->assign("cdn_list", $cdn_list);
		}

		function index($search = 0, $sort = 0, $asc_des = 0, $is_all = 1, $is_live=0, $is_verified=0, $school_id = 0, $limit = 0){
			$this->load->model("Schools");
			if($this->input->post()){
				$search = $this->input->post('search',TRUE)? urldecode($this->input->post('search',TRUE)) : 0;
				$is_live = $this->input->post('is_live',TRUE)? $this->input->post('is_live',TRUE) : 0;
				$is_verified = $this->input->post('is_verified',TRUE)? $this->input->post('is_verified',TRUE) : 0;
				$school_id = $this->input->post('school_id',TRUE)? $this->input->post('school_id',TRUE) : 0;
				$limit = 0;
			}else{
				$search = urldecode($search);
				$page_config['uri_segment'] = 10; 
			}
			
			$data = array('search' => $search, 'sort' => $sort, 'asc_des' => $asc_des, 'is_live' => $is_live, 'is_verified' => $is_verified, 'school_id' => $school_id, 'limit' => $limit);
			$this->smarty->assign("action", '会员列表');
			$offset = 10;
			$result = $this->users->get_user_list($data['limit'], $offset, $data);
			$this->load->library('pagination');
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['base_url'] = '/user/index/'.$data['search'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$is_all.'/'.$is_live.'/'.$is_verified.'/'.$school_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $offset; 
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$school_data = $this->Schools->get_school_list();
			$school_list = !empty($school_data['count']) ? $school_data['result'] : array();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("school_list", $school_data);
			$this->smarty->assign("data", $data);
			$this->smarty->assign("is_all", $is_all);
			$this->smarty->assign("user_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			
			// $public_info = get_field_by_where("public_config", "cash_ratio", "id=1");
			// $this->smarty->assign("cash_ratio", $public_info['cash_ratio']);
			$this->smarty->assign("type", 13);
	        $school_info['id'] = $school_id;
	        $this->smarty->assign("school_info", $school_info);
			$this->smarty->display("user/user_list.html");
		}

		function live_index($search = 0, $sort = 0, $asc_des = 0, $is_all = 1, $is_live=1, $team_id=0, $limit = 0){
			if($this->input->post()){
				$search = $this->input->post('search',TRUE)? urldecode($this->input->post('search',TRUE)) : 0;
				$is_live = $this->input->post('is_live',TRUE)? $this->input->post('is_live',TRUE) : 1;
				$team_id = $this->input->post('team_id',TRUE)? $this->input->post('team_id',TRUE) : 0;
				$limit = 0;
			}else{
				$search = urldecode($search);
				$page_config['uri_segment'] = 9; 
			}
			
			$data = array('search' => $search, 'sort' => $sort, 'asc_des' => $asc_des, 'is_live' => $is_live, 'team_id'=>$team_id, 'limit' => $limit);
			$this->smarty->assign("action", '直播列表');
			$offset = 10;
			$result = $this->users->get_user_list($data['limit'], $offset, $data);
			$this->load->library('pagination');
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['base_url'] = '/user/live_index/'.$data['search'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$is_all.'/'.$is_live.'/'.$team_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $offset; 
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("data", $data);
			$this->smarty->assign("is_all", $is_all);
			$this->smarty->assign("user_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			
			$public_info = get_field_by_where("public_config", "cash_ratio", "id=1");
			$this->smarty->assign("cash_ratio", $public_info['cash_ratio']);
			
			$this->smarty->display("user/user_list.html");
		}
		
		function add(){
			$this->smarty->assign("action", '添加会员');
			$this->smarty->display("user/user_save.html");
		}

		function save(){
			if( ! $this->input->post()) {
				show_message('/user', "非法操作！");exit;
			}
			$data = $this->input->post();
			if(isset($data['head_image_url'])) $data['head_image_url'] = get_qiniu_url($data['head_image_url']);
			if(isset($data['image_url'])) $data['image_url'] = get_qiniu_url($data['image_url']);
			if(isset($data['watermark'])) $data['watermark'] = get_qiniu_url($data['watermark']);
			if(!empty($data['id'])){
				unset($data['file']);
				if(!isset($data['anchor'])) $data['anchor'] = 0;
				$aa = $this->update_user_info($data);
			}else{
				unset($data['file']);
				$this->add_user($data);
			}
		}

		function add_user($data){
			$data['add_time'] = time();
			if($data['password'] != $data['confirm_password']) {
				show_message('/manage_user/add', '您两次输入的密码不一一致,请重新输入!');
			}else{
				unset($data['confirm_password']);
			}
			$md5_pass = md5($data['password']);
			$rand_str = '';
			for($i=0; $i<10; $i++){
				$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
			}
			$data['rand_str'] = $rand_str;
			$data['password'] = md5($md5_pass.$rand_str);
			$data['register_ip'] = $_SERVER["REMOTE_ADDR"];
			
			try{
				$data['stream_id'] = 123456;
				$result = $this->users->insert($data);
			} catch (Exception $e) {
				$result = false;
			}
			if($result == TRUE){
				show_message('/user', "会员信息添加成功");
			}else{
				show_message('/user', "会员信息添加失败，请重试！");
			}
		}

		function view($id){
			$user_info = $this->users->get_user_info($id);
			if( ! $user_info){
				show_message('/user', "您指定的会员不存在!");
				exit;
			}
			$this->smarty->assign("action", '查看会员信息');
			$this->smarty->assign("user_info", $user_info);
			$this->smarty->display("user/user_view.html");
		}

		function edit($id){

			//推荐广告位 1：app首页
			$recommended_place = 1;
			//类型 2：用户
			$type = 2;
			$user_info = $this->users->get_user_info($id);
			/*$recommend_info = $this->users->get_recommend_info($id,$recommended_place,$type);
			if($recommend_info){
				$user_info['app_jieshuo'] = 1;
			}*/
			if($user_info['head_image_url']){
					$image_path_name = explode('/', $user_info['head_image_url']);
					$image_name = array_pop($image_path_name);
					$image_name = explode('.', $image_name);
					$image_name = array_shift($image_name);
					// $user_info['head_image_url'] = get_thumb_image_url($user_info['head_image_url']);
				}
			if( ! $user_info){
				show_message('/user', "您指定的会员不存在!");
				exit;
			}
			$this->smarty->assign("action", '编辑会员信息');
			$this->smarty->assign("user_info", $user_info);
			$this->smarty->display("user/user_save.html");
		}

		function update_user_info($data){
			$data['edit_time'] = time();
			$this->db->where('id', $data['id']);
 			$this->db->update('user', $data);
 			show_message($_SERVER['HTTP_REFERER'], "会员信息修改成功");
		}

		function delete($id = 0){
			if($id){
				$this->db->where('id', $id);
				$result = $this->db->delete('user');
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER'], "会员信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "会员信息删除失败，请重试！");
				}
			}else{
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					$this->users->batch_delete($ids);
					show_message($_SERVER['HTTP_REFERER'], "用户信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
				}
			}
		}

		/**
		* 函数功能介绍：用户批量删除
		* 创建人: 	Yang Ningshu
		* 创建时间: 2014-09-02 13:49
		* 最后修改人：
		* 最后修改时间：
		*/
		function delete_all($id = 0) {
			if(!$this->input->post('ids')) {
				$ids = array($id);
			}else {
				$ids = $this->input->post('ids');
			}
			if($ids) {
				foreach($ids as $key=>$val) {
					$this->users->delete($val);
				}
			}
			show_message($_SERVER['HTTP_REFERER'], "视频批量删除成功");
		}
		
		function up_user_nick() {
			$this->load->model('users');
			$where = "nick=''";
			$result = $this->users->get_user_nick($where);
			if($result) {
				foreach($result as $key=>$val) {
					$this->users->update_user_nick($val['id'],"玩家:".$val['id']);
					echo $key."<br>";
				}
			}
		}


		/**
		* 函数功能介绍：会员启用禁用
		* 创建人: 	Yang Ningshu
		* 创建时间: 2014-09-04 11:29
		* 最后修改人：
		* 最后修改时间：
		*/
		function status() {
			$return = array();
			if( !$this->input->get() || !$this->input->get('id')) {
				$return = array('code'=>9,'msg'=>'非法操作！');
			}else {
				$result = $this->users->update_user_status($this->input->get('id'),array('status'=>$this->input->get('status')));
				if($result) {
					$return = array('code'=>0,'msg'=>'成功修改');
				}else {
					$return = array('code'=>1,'msg'=>'修改失败');
				}
			}
			echo json_encode($return);exit;
		}

		/**
		* 函数功能介绍：验证用户名是否存在
		* 创建人: 	Zhang Shaowei
		* 创建时间: 2015-01-21 15:29
		* 最后修改人：
		* 最后修改时间：
		*/
		function ignore_check_user() {
			$return = array();
			if( !$this->input->post()) {
				$return = array('code'=>9,'msg'=>'非法操作！');
			}else {
				$where 	= '';
				$result = '';
				if($this->input->post('username')) {
					$where = "username='". $this->input->post('username') ."'";
				}else if ($this->input->post('nick')) {
					$where = "nick='". $this->input->post('nick') ."'";
					if($this->input->post('user_id') > 0){
						$where.= ' AND id!='.$this->input->post('user_id');
					}
					$filename = "keep.txt";
					$handle = fopen($filename, "r");//读取二进制文件时，需要将第二个参数设置成'rb'
					
					//通过filesize获得文件大小，将整个文件一下子读到一个字符串中
					$badword = fread($handle, filesize ($filename));
					fclose($handle);
					if(preg_match_all("/".$badword."/i", $this->input->post('nick'),$matches)){
						$return = array('code'=>2,'msg'=>'敏感词');
						echo json_encode($return);exit;
					}
				}
				if($where) {
					$result = $this->users->get_user_nick($where);
				}
				if(!$result) {
					$return = array('code'=>1,'msg'=>'验证成功');
				}else {
					$return = array('code'=>0,'msg'=>'验证失败');
				}
			}
			echo json_encode($return);exit;
		}

		function vlist($limit = 0){
			$this->smarty->assign("action", '实名申请列表');
			$result = $this->users->get_user_vlist($limit, $this->offset);
			if(is_array($result['result'])){
				foreach($result['result'] as $rk=>$rv){
					$result['result'][$rk]['otherinfo']['nick']='';
					$otherinfo=$this->users->get_user_info($rv['uid']);
                    if($otherinfo){
                    	$result['result'][$rk]['otherinfo']=$otherinfo;
                    }

					$result['result'][$rk]['type_name']='';
					$info = get_field_by_where('user_type', '*', 'id=' . $rv['type_id']);
					if(!empty($info)){
						$result['result'][$rk]['type_name']=$info['type_name'];
					}
				}
			}
			$this->load->library('pagination');
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['base_url'] = '/user/vlist/';
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset; 
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("vlist", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("user/vlist.html");
		}

		function verified($id = 0, $status = 0){
			if( $id <= 0 || $status <= 0){
				show_message('/vlist', "非法操作!");
				exit;
			}

			$data['status'] = $status;
			$this->db->where('id', $id);
 			$this->db->update('user_idcard', $data);
 			if($this->db->affected_rows() > 0){
 				$user_info = get_field_by_where("user_idcard", "uid,type_id", "id=$id");
 				$message = '非常抱歉，您的直播认证申请被拒绝，请仔细检查您提交的资料是否符合要求。';
 				if($status == 1){
 					$message = '很高兴通知您，您的直播认证申请已通过审核，祝您使用愉快 ^-^ ';
 				}
 				$msg_data['from_uid'] = 0;
 				$msg_data['from_nick'] = '';
 				$msg_data['to_uid'] = $user_info['uid'];
 				$msg_data['content'] = $message;
 				$msg_data['msg_type'] = 9;
 				$msg_data['add_time'] = time();
 				$this->db->insert("user_message", $msg_data);
				
				$this->db->where('id', $user_info['uid']);
				
				$updatearr=array();
				
				$updatearr['verified_anchor'] = $status;
				$updatearr['type_id'] = $user_info['type_id'];
				
				$this->db->update('user', $updatearr);
 			}
 			show_message($_SERVER['HTTP_REFERER'], "操作成功");
		}
		
		function anchor_video_rtmp_play() {
			if( $this->input->post() && $this->input->post('uid')) {
				$uid=$this->input->post('uid');
				$user_info=get_field_by_where('user','cdn',"id=$uid");
				$return = $this->anchor_redis->redis_class->get_push_url($uid,$user_info['cdn']);
				if(!empty($return['publish_url'])) {
					$result = array('result_code' => 1,'msg'=> $return['publish_url']);
				}else {
					$result = array('result_code' => 0);
				}
				echo json_encode($result);exit;
			}
		}
		
		//更改直播状态
		function change_live_status(){
			$post_data = $this->input->post();
			if(empty($post_data['uid'])) {
				$result = array('result_code' => 0, 'msg' => '缺少主播ID');
			}elseif(! isset($post_data['is_live'])){
				$result = array('result_code' => 0, 'msg' => '缺少参数:is_live');
			}else{
				if($post_data['is_live'] == 0){
					//结束直播
					$result = $this->end_live($post_data);
				}elseif($post_data['is_live'] == 1){
					//开始直播
					$result = $this->start_live($post_data);
				}
			}
			exit(json_encode($result));
		}
		
		/**
		* 函数功能介绍：开始直播
		* 创建人: 	Zhang Shaowei
		* 创建时间: 2015-07-16 17:05
		* 最后修改人：Zhou Hailong
		* 最后修改时间：2015-07-29 10:35
		*/
		function start_live($post_data = array()) {
			$post_data = $this->input->post();
			if(empty($post_data['uid'])) {
				$result = array('result_code' => 0, 'msg'=> '缺少参数');
			}else {
				$uid=$post_data['uid'];
				$user_info=get_field_by_where('user','cdn',"id=$uid");
				$result = $this->anchor_redis->redis_class->start_live($post_data['uid'],$user_info['cdn']);
			}
			
			return $result;
		}

		/**
		* 函数功能介绍：结束直播
		* 创建人: 	Zhang Shaowei
		* 创建时间: 2015-07-16 17:05
		* 最后修改人：Zhou Hailong
		* 最后修改时间：2015-07-29 11:05
		*/
		function end_live($post_data = array()) {
			
			$post_data = $this->input->post();
			if(empty($post_data['uid'])) {
				$result = array('result_code' => 0, 'msg', '缺少参数');
			}else {
				$result = $this->anchor_redis->redis_class->end_live($post_data['uid']);
			}
			return $result;
		}
	}
